package com.dayuanit.dy11.mall.mallspringboot.service;

import com.dayuanit.dy11.mall.mallspringboot.dto.CatalogDTO;
import com.dayuanit.dy11.mall.mallspringboot.dto.HomeCatalogDTO;
import com.dayuanit.dy11.mall.mallspringboot.entity.Catalog;
import com.dayuanit.dy11.mall.mallspringboot.entity.Commodity;
import com.dayuanit.dy11.mall.mallspringboot.mapper.CatalogMapper;
import com.dayuanit.dy11.mall.mallspringboot.mapper.CommodityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class HomeService {

    @Value("${book.img}")
    private String picUrl;

    @Autowired
    private CatalogMapper catalogMapper;

    @Autowired
    private CommodityMapper commodityMapper;

    public List<CatalogDTO> listCatalog() {
        //TODO 方案一：
        // 1.查询所有的导航栏
        //2.从list中找到一级导航栏
        //3.再根据一级导航栏找到二级导航栏

        //TODO 方案二：
        //1.先查询一级导航栏
        //2.再循环一级导航栏集合，根据一级导航栏查询二级导航栏
        final List<Catalog> catalogs = catalogMapper.listFirstCatalog();
        List<CatalogDTO> catalogDTOs = new ArrayList<>(catalogs.size());
        for (Catalog catalog : catalogs) {
            final List<Catalog> secondCatalogs = catalogMapper.listSecondCatalog(Integer.valueOf(catalog.getCatalogNum()));
            final CatalogDTO dto = new CatalogDTO();//一级目录
            dto.setCatalogNum(catalog.getId());
            dto.setCatalogName(catalog.getCatalogName());
            dto.setParentId(Integer.valueOf(catalog.getParentNum()));

            List<CatalogDTO> secondDTOs = new ArrayList<>(secondCatalogs.size());
            for (Catalog sc : secondCatalogs) {
                CatalogDTO secondDTO = new CatalogDTO();
                secondDTO.setChilds(null);
                secondDTO.setParentId(Integer.valueOf(sc.getCatalogNum()));
                secondDTO.setCatalogName(sc.getCatalogName());
                secondDTO.setCatalogNum(sc.getId());

                secondDTOs.add(secondDTO);
            }

            dto.setChilds(secondDTOs);

            catalogDTOs.add(dto);
        }

        return catalogDTOs;
    }

    public List<HomeCatalogDTO> listHomeCommoditys() {
        //TODO 查询所以的一级目录
        final List<Catalog> catalogs = catalogMapper.listFirstCatalog();
        List<HomeCatalogDTO> homeCatalogDTOs = new ArrayList<>(catalogs.size());

        //TODO 根据一级目录查询12个商品
        for (Catalog catalog : catalogs) {
            final List<Commodity> commodities = commodityMapper.listHomeCommos(catalog.getCatalogNum());
            HomeCatalogDTO homeCatalogDTO = new HomeCatalogDTO();
            homeCatalogDTO.setCatalogName(catalog.getCatalogName());
            homeCatalogDTO.setCatalogNum(catalog.getCatalogNum());

            List<HomeCatalogDTO.HomeCommodity> commodityList = new ArrayList<>(commodities.size());
            for (Commodity commodity : commodities) {
                HomeCatalogDTO.HomeCommodity homeCommodity = new HomeCatalogDTO.HomeCommodity();
                homeCommodity.setCommoId(commodity.getId());
                String bookName = commodity.getCommoName();
                homeCommodity.setCommoName(bookName.length() > 13 ? bookName.substring(0, 13) : bookName);
                homeCommodity.setPicUrl(picUrl + commodity.getPictureUrl());
                homeCommodity.setPrice(commodity.getPrice());

                commodityList.add(homeCommodity);
            }

            homeCatalogDTO.setCommons(commodityList);
            homeCatalogDTOs.add(homeCatalogDTO);
        }

        return homeCatalogDTOs;
    }

}
